package com.sinya.shoppingmall.widget.timeline;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.sinya.shoppingmall.R;
import com.sinya.shoppingmall.bean.OrderBean;
import com.sinya.shoppingmall.utils.DateUtils;
import com.vipul.hp_hp.timelineview.TimelineView;

import java.util.List;

/**
 * Created by HP-HP on 05-12-2015.
 */
public class TimeLineAdapter extends RecyclerView.Adapter<TimeLineViewHolder> implements View.OnClickListener {

    private List<OrderBean> mFeedList;
    private Context context;

    //2.接口成员化
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public TimeLineAdapter(Context context, List<OrderBean> feedList) {
        this.context = context;
        mFeedList = feedList;
    }
    public void updateList(List<OrderBean> feedList) {
        mFeedList = feedList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return TimelineView.getTimeLineViewType(position, getItemCount());
    }

    @Override
    public TimeLineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_timeline, null);
        view.setOnClickListener(this);
        return new TimeLineViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(TimeLineViewHolder holder, int position) {

        OrderBean timeLineModel = mFeedList.get(position);
        //将每个item数据保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(timeLineModel);
        holder.mOrderId.setText("订单编号:" + timeLineModel.orderId);
        holder.mOrderPrice.setText(timeLineModel.price + "");
        holder.mOrderStatus.setText(timeLineModel.status);

        String deliveryDate = DateUtils.transferLongToDate("yyyy-MM-dd HH:mm:ss", Long.valueOf(timeLineModel.time));
        holder.mOrderTime.setText(deliveryDate);

    }

    @Override
    public int getItemCount() {
        return (mFeedList!=null? mFeedList.size():0);
    }

    //1.创建接口
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , Object data);
    }

    //4.调用接口
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取item自身数据
            mOnItemClickListener.onItemClick(v, (Object)v.getTag());
        }
    }

    //2.暴露方法，接收外界传进来的接口
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
